package com.tust;

import com.tust.service.*;
import com.tust.db.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;

public class ApplicationStart {

    private static Connection connection;

    static {
        connection = DBUtil.getConnection();
    }

    public static void main(String[] args) {
        if (connection == null) {
            System.out.println("数据库连接失败");
            return;
        }

        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("请输入所要操作类别：A->课程，B->教师,C->学生,D->任课,E->选课,F->成绩，EXIT->结束");
            try {
                String type = scanner.next();
                if (type.equalsIgnoreCase("A")) {
                    new CourseServiceImpl().service(connection);
                } else if (type.equalsIgnoreCase("B")) {
                    new TeacherServiceImpl().service(connection);
                } else if (type.equalsIgnoreCase("C")) {
                    new StudentServiceImpl().service(connection);
                } else if (type.equalsIgnoreCase("D")) {
                    new TeacherCourseServiceImpl().service(connection);
                } else if (type.equalsIgnoreCase("E")) {
                    new StudentCourseServiceImpl().service(connection);
                } else if (type.equalsIgnoreCase("F")) {
                    new ScoreServiceImpl().service(connection);
                } else if (type.equalsIgnoreCase("EXIT")) {
                    try {
                        if (connection != null) {
                            connection.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    if (scanner != null) {
                        scanner.close();
                    }
                    break;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
